﻿using System.Collections;
using System.Collections.Generic;
using Assets.Scripts;
using UnityEngine;

public class AIController : MonoBehaviour
{
    private static AIController _instance;

    public static AIController Instance
    {
        get { return _instance; }
    }

    private void Awake()
    {
        _instance = this;
        InitWidget();
    }

    private void Start()
    {
        Init();
    }

    /// <summary>
    /// AI的球拍.
    /// </summary>
    private Racket _aiRacket;

    /// <summary>
    /// 获取组件函数.
    /// </summary>
    private void InitWidget()
    {
        _aiRacket = GameObject.Find("AIRacket").GetComponent<Racket>();
    }

    /// <summary>
    /// 初始化函数.
    /// </summary>
    private void Init()
    {
        _dstPosition = _aiRacket.transform.position;
    }

    /// <summary>
    /// 每隔多久思考一次.
    /// </summary>
    [SerializeField]
    private float _thinkTimeOfIdle = 2.0f;

    //思考时间计时器.
    private float _thinkTimeCounter = 0;

    /// <summary>
    /// 下一步需要移动到的位置.
    /// </summary>
    private Vector2 _dstPosition;

    /// <summary>
    /// 移动速度.
    /// </summary>
    [SerializeField]
    private float _aiMoveSpeed = 1.0f;

    private void Update()
    {
        if (!GameManager.Instance.IsGaming)
        {
            //还没有开始游戏则不执行.
            return;
        }
        _thinkTimeCounter += Time.deltaTime;
        if (_thinkTimeCounter > _thinkTimeOfIdle)
        {
            //进行一次判断.
            _dstPosition = GetDestination();
            Debug.Log("下一步AI需要移动到的位置：" + _dstPosition);

            //清空计时器.
            _thinkTimeCounter = 0;
        }
        //插值移动到目标位置.
        _aiRacket.transform.position = Vector2.Lerp(_aiRacket.transform.position, _dstPosition,
            _aiMoveSpeed * Time.deltaTime);
        //TODO:AI可以攻击.
        
    }

    //场上的球.
    private GameObject _ball;

    /// <summary>
    /// 设置当前场上的球.
    /// </summary>
    /// <param name="ball">球的实体</param>
    public void SetBall(GameObject ball)
    {
        _ball = ball;
    }

    //顶端上限.
    private const float EdgeTop = 5.0f;
    //底端上限.
    private const float EdgeBottom = -5.0f;

    /// <summary>
    /// 获取AI球拍需要移动到的位置.
    /// </summary>
    /// <returns>目标位置</returns>
    private Vector2 GetDestination()
    {
        //球的位置.
        Vector2 ballPos = _ball.transform.position;
        //移动方向.
        Vector2 moveDir = _ball.GetComponent<Rigidbody2D>().velocity;
        //目标位置.
        Vector2 dstPos = new Vector2 { x = _aiRacket.transform.position.x };
        dstPos.y = (moveDir.y / moveDir.x) * (dstPos.x - ballPos.x) + ballPos.y;
        //修正目标位置.
        dstPos = ProcessDst(dstPos);
        return dstPos;
    }

    /// <summary>
    /// 处理目标位置.
    /// </summary>
    /// <param name="pos">目标位置</param>
    /// <returns>修正后的值</returns>
    private Vector2 ProcessDst(Vector2 pos)
    {
        Vector2 dst = pos;
        //大于上边界.
        if (pos.y > EdgeTop)
        {
            //相对于上边界倒转.
            dst.y = 2 * EdgeTop - pos.y;
        }
        else if (pos.y < EdgeBottom)
        {
            //相对于下边界倒转.
            dst.y = 2 * EdgeBottom - pos.y;
        }
        else
        {
            //在标定范围内，合法.
            return dst;
        }

        return ProcessDst(dst);
        
    }
}
